#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "game.h"


int main(void)
{	
	FILE *fp_login = fopen("user.txt", "r");
	struct user user_login;
	struct user list[USER_MAX];

	int login_choice;
	int game_choice;
	int target;
	int end_choice;
	int jump_program;
	int time;
	int login_judge;
	char account_temp[10];
	char password_temp[10];

	start();
	
	while (1) {
		if (fp_login == NULL) {
			printf("FILE NOT FOUND");
			return -1;
		}
		for (time = 0; time < USER_MAX; time++) {
			fscanf(fp_login, "%s%s", account_temp, password_temp);
			strcpy(list[time].account, account_temp);
			strcpy(list[time].password, password_temp);
		}
		login_choice = login_menu();
		if (login_choice == 1){
			register_account(list);
			continue;
			}
		if (login_choice == 2) {
			user_login = login();
			login_judge = exist(list, user_login);
			if (login_judge == 1)
				break;
			else {
				system("clear");
				printf("\n\t\t非法用户\n\t亲触回车键返回菜单\n");
				getchar();
				getchar();
				setbuf(stdin, NULL);	
				continue;
			}
		}
		if (login_choice == 3)
			return 0;

	}
	
	while (1) {
		game_choice = menu();
		if (game_choice == 1)
			target = randnum(RANGE_FAST);
		if (game_choice == 2)
			target = randnum(RANGE_GENERAL);
		if (game_choice == 3) {
			ruler();
			continue;
		}
		if (game_choice == 4)
			break;
			
		jump_program = game(game_choice, target);
		if (jump_program == 1)
			continue;
		if (jump_program == 2)
			break;
		else
			end_choice=end();
			if (end_choice == 1)
				break;
			else
				continue;
	}
	system("clear");
		
	return 0;
}

